//
//  IBaseViewController.m
//  KangarooApp
//
//  Created by zhuangyihang on 7/15/15.
//  Copyright (c) 2015 KangarooFamily. All rights reserved.
//

#import "IBaseViewController.h"
#import <Masonry/Masonry.h>
#define BUTTON_MARGIN 5

@interface IBaseViewController()

@property (nonatomic, assign) UIStatusBarStyle barStyle;

@end

@implementation IBaseViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.baseNavigationBarView = [UIView new];
    [self.view addSubview:self.baseNavigationBarView];
    [self.baseNavigationBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.mas_width);
        make.top.mas_equalTo(self.view);
        make.centerX.equalTo(self.view);
    }];
    
    self.baseContentView = [UIView new];
    [self.view addSubview:self.baseContentView];
    [self.baseContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.mas_width);
        make.top.mas_equalTo(self.baseNavigationBarView.mas_bottom);
        make.bottom.equalTo(self.view);
        make.centerX.equalTo(self.view);
    }];
    
    [self setBaseNavigationBarHidden:NO];
    [self setupTitleAndButton];
}

#pragma mark - Private
- (void)setupTitleAndButton{
    self.baseLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseNavigationBarView addSubview:self.baseLeftButton];
    self.baseRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseNavigationBarView addSubview:self.baseRightButton];
    
    [self.baseLeftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(IAppNavigationBarHeight, IAppNavigationBarHeight));
        make.left.equalTo(self.baseNavigationBarView).offset(BUTTON_MARGIN);
        make.bottom.equalTo(self.baseNavigationBarView);
    }];
    
    [self.baseRightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(IAppNavigationBarHeight);
        make.height.mas_equalTo(IAppNavigationBarHeight);
        make.right.equalTo(self.baseNavigationBarView).offset(-BUTTON_MARGIN);
        make.bottom.equalTo(self.baseNavigationBarView);
    }];
    
    self.baseNavigationBarLabel = [UILabel new];
    self.baseNavigationBarLabel.textAlignment = NSTextAlignmentCenter;
    [self.baseNavigationBarView addSubview:self.baseNavigationBarLabel];
    [self.baseNavigationBarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.baseLeftButton.mas_right);
//        make.right.mas_equalTo(self.baseRightButton.mas_left);
        make.centerX.equalTo(self.baseNavigationBarView);
        make.height.mas_equalTo(IAppNavigationBarHeight);
        make.bottom.mas_equalTo(self.baseNavigationBarView);
        make.width.mas_equalTo(@200);
    }];

}

#pragma mark - Public
- (void)setBaseNavigationBarHidden:(BOOL)hidden{
    if (hidden) {
        [self.baseNavigationBarView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(CGFLOAT_MIN);
        }];
    }else{
        [self.baseNavigationBarView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@(IAppNavigationBarHeight+IAppStatusBarHeight));
        }];
    }
}

- (void)setBaseNavigationBarTitle:(NSString *)barTitle{
    self.baseNavigationBarLabel.text = barTitle;
}

- (void)setBaseNavigationBarColor:(UIColor *)barColor{
    self.baseNavigationBarLabel.textColor = barColor;
}

- (void)setBaseNavigationBarBackgroundColor:(UIColor *)barColor{
    self.baseNavigationBarView.backgroundColor = barColor;
}

- (void)updateLeftButtonWidth:(CGFloat)width{
    [self.baseLeftButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
}

- (void)updateRightButtonWidth:(CGFloat)width{
    [self.baseRightButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- (void)addLeftButtonSelector:(SEL)selector{
    [self.baseLeftButton removeTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [self.baseLeftButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
}
- (void)addRightButtonSelector:(SEL)selector{
    [self.baseRightButton removeTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [self.baseRightButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
}
#pragma clang diagnostic pop

- (void)setLeftButtonImage:(UIImage *)image{
    [self.baseLeftButton setImage:image forState:UIControlStateNormal];
}
- (void)setRightButtonImage:(UIImage *)image{
    [self.baseRightButton setImage:image forState:UIControlStateNormal];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return self.barStyle;
}

- (void)setStatusBarStyle:(UIStatusBarStyle)style{
    self.barStyle = style;
    [self setNeedsStatusBarAppearanceUpdate];
}

@end
